www.gusucode.com > VC++ 信息加密聊天源代码-源码程序 > VC++ 信息加密聊天源代码-源码程序\code\SecChat\ClientSocket.cpp

    // ClientSocket.cpp : implementation file
// download by http://www.NewXing.com

#include "stdafx.h"
#include "SecretChat.h"		//应用程序类头文件
#include "ClientSocket.h"
#include "SecretChatDlg.h"	//主窗口类头文件

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_CANCELINSTANCE ( WM_USER + 2)	//关闭窗口消息
/////////////////////////////////////////////////////////////////////////////
// CClientSocket


CClientSocket::CClientSocket()
{
}

CClientSocket::~CClientSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::OnReceive(int nErrorCode)	//接收服务器数据事件
{	
	if(0 != nErrorCode)
	{	//发送错误
		AfxMessageBox("接收数据出错-1");
		return;
	}

	CSecretChatDlg * pMain = (CSecretChatDlg *)AfxGetMainWnd();
	MessagePackage msg;
	int n;					//实际接收到数据的字节数
	n = Receive(&msg, sizeof(MessagePackage));	//接收客户机数据

	if(0 > n)
	{	//发送错误
		AfxMessageBox("接收数据出错-2");
		return;
	}

	//错误提示
	pMain->CuoWuTiShi();

	/**********当收到的是对方发过来的消息************/
	if(msg.head == HEAD_TEXT) //收到的是正文
	{
		pMain->Receive(msg);	//在这里会调用接收消息的线程
	}

	else if(msg.head == HEAD_REVERT_TEXT) //发出的正文就收到的是回复
	{
		pMain->ReceiveRevert(msg);
	}

	else if(msg.head == HEAD_DISCONNECTION) //收到的是断开提示
	{
		pMain->disconnection_message(msg);
	}

	else if(msg.head == HEAD_ZAIXIANBIAOJI)		//在线通知
	{
		pMain->m_zai_xian_biao_ji = 0;	//取消等待时间记录
	}

	else if(msg.head == HEAD_VERSION)		//对方版本号和一些其他信息
	{	
		pMain->message_version(msg);
	}

	else if(msg.head == HEAD_CLAIM_PUBLIC_KEY) //对方的请求获得公钥
	{
		pMain->ClaimPublicKey(msg);
	}

	else if(msg.head == HEAD_REVERT_PUBLIC_KEY) //收到对方发过来的公钥
	{		
		pMain->RevertPublicKey(msg);
	}

	else if(msg.head == HEAD_DIGITAL_SIGNATURE) //对方的数字签名,看看我这里有没有它的公钥,可以在请求获得它的公钥
	{
		if(msg.ID == HEAD_DIGITAL_SIGNATURE_NO)
		{	//对方还没识别我这条消息的身份
			MessageBox(
				NULL,
				"对方不是 " 
				+ pMain->GetFriendName() 
				+ " 好友或没有 " 
				+ pMain->GetUserName() 
				+ " 用户的公钥",
				"发送失败身份不确认",
				MB_ICONEXCLAMATION);
		}
	}

	else if(msg.head == HEAD_SENDFILE) //对方发来了文件及相关信息
	{
		pMain->receive_file(msg);
	}

	/******************************/

	//要求读、写、关闭通知,会出错
//	AsyncSelect(FD_READ | FD_WRITE | FD_CLOSE);
}